package com.marvels.financial.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import com.fintech.third.utils.ThirdSystemUtil;
import com.marvels.common.utils.MarvelsLogUtil;

@Component
public class ExcelToPdfUtil {
	
	private static String configPropertiesPath;
	
	
	public  String getConfigPropertiesPath() {
		return configPropertiesPath;
	}
	
	@Value("${config.properties.path}")
	public  void setConfigPropertiesPath(String configPropertiesPath) {
		ExcelToPdfUtil.configPropertiesPath = configPropertiesPath;
	}
	
	
	/**
	 * 支持DOC, DOCX, OOXML, RTF, HTML, OpenDocument, PDF, EPUB, XPS, SWF等相互转换<br>
	 * 
	 * @param args
	 */
	public static void excel2PDF(String excelPath, String pdfPath) {
		// 验证License
		if (!getLicense()) {
			return;
		}
		FileOutputStream fileOS = null;
		try {
			long old = System.currentTimeMillis();
			Workbook wb = new Workbook(excelPath);// 原始excel路径
			File pdfFile = new File(pdfPath);// 输出路径
			fileOS = new FileOutputStream(pdfFile);
			wb.save(fileOS, SaveFormat.PDF);
			long now = System.currentTimeMillis();
			MarvelsLogUtil.getInstance().info("共耗时：" + ((now - old) / 1000.0) + "秒");
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>excel转pdf异常:",e);
		} finally {
			try {
				if(null!=fileOS){
					fileOS.close();
				}
			} catch (IOException e) {
				MarvelsLogUtil.getInstance().error(">>>>>>流关闭异常:",e);
			}
		}
	}

	public static boolean getLicense() {
		boolean result = false;
		try {
			String filePath=configPropertiesPath+ File.separator+"asposelicense.xml";
			File loadFile = new File(filePath);
			InputStream in = new FileInputStream(loadFile);
			License aposeLic = new License();
			aposeLic.setLicense(in);
			result = true;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>获取证书异常:",e);
		}
		return result;
	}

}
